﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;

namespace Commerce.CSK.Web.Controls
{
    public partial class RequiredTextBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override string ToString()
        {
            return this.Text; 
        }

        public string CssClass
        {
            get { return this.fieldTextBox.CssClass; }
            set { this.fieldTextBox.CssClass = value; }
        }

        public int MaxLength
        {
            get { return this.fieldTextBox.MaxLength; }
            set { this.fieldTextBox.MaxLength = value; }
        }

        [DefaultValue(150)]
        public Unit Width
        {
            get { return this.fieldTextBox.Width; }
            set { this.fieldTextBox.Width = value; }
        }

        public TextBox ControlTextBox
        {
            get { return this.fieldTextBox; }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            //Final checks. 
            regExValidator.Enabled = !String.IsNullOrEmpty(ValidationExpression);
            fieldLabel.Visible = !String.IsNullOrEmpty(LabelText);
            watermarkExtender.Enabled = !String.IsNullOrEmpty(WatermarkText); 
        }

        [Bindable(true)]
        public string Text
        {
            get
            {
                return fieldTextBox.Text; 
            }

            set
            {
                fieldTextBox.Text = value; 
            }
        }

        public string WatermarkText
        {
            get
            {
                return watermarkExtender.WatermarkText;
            }

            set
            {
                watermarkExtender.WatermarkText = value;
                if (String.IsNullOrEmpty(this.RequiredErrorText))
                {
                    this.RequiredErrorText = WatermarkText + " is a required value.";
                }
                if (string.IsNullOrEmpty(this.ValidationErrorText))
                {
                    this.ValidationErrorText = WatermarkText + " is in the wrong format."; 
                }
            }
        }
        
        public string LabelText
        {
            get
            {
                return fieldLabel.Text; 
            }

            set
            {
                fieldLabel.Text = value;
            }
        }

        public bool Required
        {
            get { return requiredFieldValidator.Enabled; }
            set { requiredFieldValidator.Enabled = value; }
        }

        public string RequiredErrorText
        {
            get { return requiredFieldValidator.ErrorMessage; }
            set { requiredFieldValidator.ErrorMessage = value; }
        }

        public string ValidationExpression
        {
            get
            {
                return regExValidator.ValidationExpression;
            }
            set
            {
                regExValidator.ValidationExpression = value; 
            }
        }

        public string ValidationErrorText
        {
            get
            {
                return regExValidator.ErrorMessage;
            }
            set
            {
                regExValidator.ErrorMessage = value; 
            }
        }

    }
}